package com.xywh.emergency_outbound.mvp.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xywh.emergency_outbound.R;
import com.xywh.emergency_outbound.mvp.ui.entity.Tab4;

import java.util.List;

import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;


public class Tab4Adapter extends BaseQuickAdapter<Tab4.DataBean, BaseViewHolder> {
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    private Context mContext;
    private AddressDel mAddressDel;

    public Tab4Adapter(Context context, @Nullable List<Tab4.DataBean> data, AddressDel addressDel) {
        super(R.layout.item_tab4, data);
        this.mContext = context;
        this.mAddressDel = addressDel;
    }

    @Override
    protected void convert(BaseViewHolder helper, Tab4.DataBean item) {
        //GlideUtil.loadImage(mContext,item.getImg(),helper.getView(R.id.img_head));
        helper.setText(R.id.tv_name, item.getConsigneeName());
        helper.setText(R.id.tv_phone, item.getLinkPhone());
        helper.setText(R.id.tv_address, item.getEmgConsigneeAddresses().get(0).getAddress());
        TextView tv_mo_ren = helper.getView(R.id.tv_mo_ren);
        if (item.isIsDefault() == true) {
            tv_mo_ren.setVisibility(View.VISIBLE);
        } else {
            tv_mo_ren.setVisibility(View.GONE);
        }
        TextView tv_edit = helper.getView(R.id.tv_edit);
        TextView tv_delete = helper.getView(R.id.tv_delete);

        if (!TextUtils.isEmpty(item.getIsMixSpot()) && item.getIsMixSpot().equals("true")) {
            tv_edit.setVisibility(View.GONE);
            tv_delete.setVisibility(View.GONE);
            tv_mo_ren.setVisibility(View.GONE);
        }


        TextView tv_rong_he_qu_or_qi_ta = helper.getView(R.id.tv_rong_he_qu_or_qi_ta);
        if (!TextUtils.isEmpty(item.getIsMixSpot()) && item.getIsMixSpot().equals("true")) {
            tv_rong_he_qu_or_qi_ta.setText("(融合区)");
        } else {
            tv_rong_he_qu_or_qi_ta.setText("(其他)");
        }


        helper.setOnClickListener(R.id.tv_edit, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("address_item", item);
                Utils.navigation(mContext, RouterHub.ACTIVITY_ADDRESS_EDIT, bundle);

            }
        });
        helper.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageNegativeDialog(item);
            }
        });


    }

    private void showMessageNegativeDialog(Tab4.DataBean item) {
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setTitle("删除地址")
                .setMessage("确定要删除吗？")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "删除", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        mAddressDel.addressDel(item);
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    public interface AddressDel {

        void addressDel(Tab4.DataBean item);
    }

}
